package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    /**
     * 客户删除（支持批量删除）
     */
    @PostMapping("/delete")
    public ServiceVO deleteCustomer(@RequestParam("ids") String ids){
        customerService.deleteCustomer(ids);

        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }


    /**
     * 客户列表分页展示
     * @param page
     * @param rows
     * @param customerName
     * @return
     */
    @PostMapping("/list")
    public Map<String,Object> list(@RequestParam("page") Integer page,
                                   @RequestParam("rows") Integer rows,
                                   @Param("customerName") String customerName){
        List<Customer> list = customerService.selectCustomerListByName(customerName);
        PageHelper.startPage(page,rows);
        PageInfo<Customer> pageInfo = new PageInfo<>(list);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageInfo.getTotal());
        map.put("rows",pageInfo.getList());
        return map;
    }

    /**
     * 客户添加或修改
     */
    @PostMapping("/save")
    public ServiceVO save(@Param("customerId") Integer customerId,
                          @RequestParam("customerName") String customerName,
                          @RequestParam("contacts") String contacts,
                          @RequestParam("phoneNumber") String phoneNumber,
                          @RequestParam("address") String address,
                          @RequestParam("remarks") String remarks){
        Customer customer = new Customer();
        customer.setCustomerId(customerId);
        customer.setCustomerName(customerName);
        customer.setContacts(contacts);
        customer.setPhoneNumber(phoneNumber);
        customer.setAddress(address);
        customer.setRemarks(remarks);
        customerService.saveAndUpdate(customer);
        ServiceVO serviceVO = new ServiceVO(100,"请求成功",null);
        return serviceVO;
    }

}
